import axios from "axios";
import  {getUserStore} from "@/service/common.js";
import router from "@/router/index.js";
import {ElMessage} from "element-plus";
const instance = axios.create({
  baseURL: '/api',
  timeout: 1000 * 5,
});
instance.interceptors.request.use(config => {
    if (getUserStore().token){
        config.headers.Authorization = `${getUserStore().token}`
    }
    return config
}, error => {
    ElMessage.error(error.message)
    router.push('/login').then(routerMessage =>{
        console.log(routerMessage)
    })
})
instance.interceptors.response.use(response => {
    const {status,success,data} = response.data
    if (status === 200 && success){
        return data
    }else {
        if (response.data.message){
            ElMessage.error(response.data.message)
        }
        //ElMessage.error(response.data.message)
    }
}, error => {
    router.push('/login').then(routerMessage =>{
        console.log(routerMessage)
    })
    //ElMessage.error(error.message)
})
export default instance
